﻿/// <summary>
/// Developer: CHOOI KAH WAI
/// Last Modified: 16/11/2009
/// </summary>

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using ARSS.Common;
using ARSS.Host.Common;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace ARSS.Host.Visual
{
	/// <summary>
	/// Interaction logic for SessionWindow.xaml
	/// </summary>
	public partial class SessionWindow : Window
	{

		public SessionWindow()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
            Loaded += new RoutedEventHandler(SessionWindow_Loaded);

		}

        /// <summary>
        /// SessionWindow routed event handler for Loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SessionWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Setup SceneNodeTreeView binding
            System.Collections.ObjectModel.ObservableCollection<SceneNode> nodes = new System.Collections.ObjectModel.ObservableCollection<SceneNode>();
            nodes.Add(Core.Instance.RootSceneNode);
            SceneNodeTreeView.ItemsSource = nodes;

        }

        /// <summary>
        /// ApplySceneButton click event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		protected void ApplySceneButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            if(SceneComboBox.SelectedValue != null)
                Core.Instance.SetScene(SceneComboBox.SelectedValue.ToString());
		}

        /// <summary>
        /// ForceRelease button click event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ForceRelease_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (SceneNodeTreeView.SelectedValue != null)
            {
                string name = (SceneNodeTreeView.SelectedValue as SceneNode).Name;
                if (name.Contains(" <"))
                {
                    name = name.Remove(name.IndexOf(" <"));
                    Core.Instance.ReleaseSceneNode(name);
                }
            }
        }

        private void RecordButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if(!Core.Instance.IsRecording)
            {
                Core.Instance.IsRecording = true;
                RecordButton.Content = "Stop Recording";
                Core.Instance.TempSceneData = new ObservableCollection<KeyFrame>();
            }
            else
            {
                Core.Instance.IsRecording = false;
                RecordButton.Content = "Record Scene";

                RecordWindow rw = new RecordWindow();
                Nullable<bool> result = rw.ShowDialog();

                if (result == true)
                {
                    MessageBox.Show("Scene recorded.");

                    XElement components = XElement.Load(Core.Instance.SceneFilePath);
                    XElement animationSet = components.Element("Animations");

                    XElement setElement = new XElement("Set", new XAttribute("name", rw.TitleTextBox.Text));
                    foreach (KeyFrame kf in Core.Instance.TempSceneData)
                    {
                        XElement keyFrameElement = new XElement("KeyFrame");
                        keyFrameElement.Add(new XAttribute("tsec", kf.TSec.ToString("0.00")));
                        foreach (AnimationNode animNode in kf.Nodes)
                        {
                            XElement animElement = new XElement("AnimationNode");
                            animElement.Add(new XAttribute("target", animNode.Target));
                            animElement.Add(new XAttribute("x", animNode.X));
                            animElement.Add(new XAttribute("y", animNode.Y));
                            animElement.Add(new XAttribute("z", animNode.Z));
                            keyFrameElement.Add(animElement);
                        }
                        setElement.Add(keyFrameElement);
                    }
                    animationSet.Add(setElement);
                    components.Save(Core.Instance.SceneFilePath);
                }
            }
            
        }
	}
}